package com.lalosoft.gastostarjetaweb.servlet;

import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lalosoft.gastostarjetaweb.Constants;
import com.lalosoft.gastostarjetaweb.dao.EmailDao;
import com.lalosoft.gastostarjetaweb.model.Email;

@SuppressWarnings("serial")
public class EliminarEmailServlet extends HttpServlet {
	
	private static final Logger log = Logger.getLogger("EliminarEmailServlet");
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		String emailId = req.getParameter("id");
		
		if(emailId.equals("0")){
			log.log(Level.WARNING, "Se han eliminado todos los mails" + emailId);
			
		} else {
			EmailDao.deleteEmail(Long.valueOf(emailId));
			log.log(Level.WARNING, "Email eliminado - Id: " + emailId);
		}
		
		List<Email> list = EmailDao.findByState(Constants.ESTADO_RECIBIDO);
		req.setAttribute("emails", list);
	    RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher("/inbox.jsp");
	    requestDispatcher.forward(req, resp);
	}

}
